import axios from 'axios';
import base from './config';
// const baseURL = 'http://localhost:8080';
const baseURL= `${base}`

// 搜索种子
export const searchTorrents = async (keyword='') => {
  try {
    const response = await axios.get(`${baseURL}/torrent/search`, {
      params: { keyword } // Include the keyword in the query parameters
    });
    return response.data;
  } catch (error) {
    console.error('搜索种子失败:', error);
    throw error;
  }
};

// 获取种子列表
export const listReviewTorrents = async (page=1,pageSize=12) => {
  try {
    const response = await axios.get(`${baseURL}/torrent/list`, {
      params: {
        page,
        pageSize
      }
    });
    return response.data;
  } catch (error) {
    console.error('获取种子列表失败:', error);
    throw error;
  }
};


// 获取种子详情
export const fetchTorrentDetail = async (id) => {
  try {
    const response = await axios.get(`${baseURL}/torrent/${id}`);
    return response.data;
  } catch (error) {
    console.error('获取种子详情失败:', error);
    throw error;
  }
};

// 获取待审核的种子列表
export const fetchPendingTorrents = async (page=1,pageSize=12) => {
  try {
    const response = await axios.get(`${baseURL}/torrent/review/list`, {
      params: {
        page,
        pageSize
      }
    });
    return response.data; // Assuming the response is in the expected format
  } catch (error) {
    console.error('获取待审核种子列表失败:', error);
    throw error;
  }
};

// 审核种子
export const reviewTorrent = async (infoHash, isApproved) => {
  try {
    const response = await axios.post(`${baseURL}/torrent/review/${infoHash}`, null, {
      params: { isApproved }
    });
    return response.data;
  } catch (error) {
    console.error('审核种子失败:', error);
    throw error;
  }
};

  // 删除种子  
  // todo
  export const removeTorrent = async (torrentId) => {
    try {
      const response = await axios.get(`${baseURL}/torrent/${torrentId}/delete`);
      return response.data;
    } catch (error) {
      console.error('下架种子失败:', error);
      throw error;
    }
  };
